package com.zxp.zxpsmarthomeapk.view.activity;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;

import com.roughike.bottombar.BottomBar;
import com.roughike.bottombar.OnTabSelectListener;
import com.zxp.zxpsmarthomeapk.R;
import com.zxp.zxpsmarthomeapk.base.CommentActivity;
import com.zxp.zxpsmarthomeapk.view.fragment.HomePageFragment;
import com.zxp.zxpsmarthomeapk.view.fragment.ModeFragment;
import com.zxp.zxpsmarthomeapk.view.fragment.RoomFragment;
import com.zxp.zxpsmarthomeapk.view.fragment.SafeGuardFragment;
import com.zxp.zxpsmarthomeapk.view.fragment.UserFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class MainActivity extends CommentActivity {

    @BindView(R.id.vpager)
    ViewPager vpager;

    @BindView(R.id.bbl_projects)
    BottomBar bbl_projects;

    private List<Fragment> list;

    @Override
    protected int getLayoutResourceId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initData() {
    }

    @Override
    protected void initView() {
        initTopBar();
        initViewPager();
    }

    private void initViewPager() {
        list = new ArrayList<Fragment>();
        HomePageFragment homePageFragment = new HomePageFragment();
        SafeGuardFragment safeGuardFragment = new SafeGuardFragment();
        RoomFragment roomFragment = new RoomFragment();
        ModeFragment modeFragment = new ModeFragment();
        UserFragment userFragment = new UserFragment();

        //添加到数组中
        list.add(homePageFragment);
        list.add(roomFragment);
        list.add(safeGuardFragment);
        list.add(modeFragment);
        list.add(userFragment);

        vpager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return list.get(position);
            }

            @Override
            public int getCount() {
                return list.size();
            }
        });

        vpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                bbl_projects.selectTabAtPosition(position, true);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void initTopBar() {
        bbl_projects.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelected(int tabId) {
                if (tabId == R.id.tab_home_page) {
                    vpager.setCurrentItem(0);
                } else if (tabId == R.id.tab_room) {
                    vpager.setCurrentItem(1);
                } else if (tabId == R.id.tab_safe) {
                    vpager.setCurrentItem(2);
                } else if (tabId == R.id.tab_mode) {
                    vpager.setCurrentItem(3);
                } else if (tabId == R.id.tab_user) {
                    vpager.setCurrentItem(4);
                }
            }
        });
    }
}
